package com.lxf.web;

import com.lxf.bean.Users;
import com.lxf.service.Impl.UsersServiceImpl;
import com.lxf.service.UsersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接受参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("前端数据："+username+"--"+password);
        //2、调取service
        UsersService usersService = new UsersServiceImpl();
        Users users = usersService.login(username, password);
        //3、跳转页面
        if (users == null) {
            //以弹窗形式告知用户 登录失败
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('登录失败');</script>");
        } else {
            //跳转到主页面
            req.getSession().setAttribute("users",users);
            resp.sendRedirect("index.jsp");
        }


    }
}
